//
//  main.swift
//  Study002_String
//
//  Created by yao_yu on 14-8-11.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import Foundation

extension YY{
    class StringTest{
    }
}

extension YY.StringTest{
    func test(){
        test_join_split()
        test_strip()
        test_sub()
        test_sub2()
    }
    
    private func test_join_split(){
        let sa = ["1","2","3","4"]
        var s1 = "".join(sa)
        assert(s1 == "1234", "join不成功")
        s1 = ".".join(sa)
        assert(s1 == "1.2.3.4", "join不成功")
        let sa2 = s1.split(".")
        assert(sa == sa2, "split不成功")
    }
    
    private func test_strip(){
        let origin = "中华人民共和国"
        var value = " \n\r\t" + origin
        assert(value.lstrip() == origin, "清除左侧空白失败")
        assert(value.strip() == origin, "清除前后空白失败")
        var value2 = " \n\r\t" + origin + " \r\t\n"
        assert(value2.rstrip() == value, "清除左侧空白失败")
        assert(value2.strip() == origin, "清除前后空白失败")
    }
    
    private func test_sub(){
        let _p1:NSString = "12"
        let _s1:NSString = "9876543210"
        let origin:NSString = _p1 + "中华人民共和国" + _s1
        var b = "\(_p1.length),\(_s1.length),\(origin.length)"
        //println(origin.substringFromIndex(9))
        
        //        var p1 = origin.sub.first(_p1.length)
        //        assert(p1 == _p1, "取前n个字符失败")
        //        var s1 = origin.last(_s1.length)
        //        assert(s1 == _s1, "取后n个字符失败")
        //
        //        s1 = origin.from(origin.length - _p1.length)
        //        assert(s1 == _s1, "取后n个字符失败")
        
    }
    
    private func test_sub2(){
        let _p1 = "12"
        let _s1 = "9876543210"
        let origin = _p1 + "中华人民共和国" + _s1
        //var b = "\(_p1.length),\(_s1.length),\(origin.length)"
        var n = origin.length - _s1.length

        
        var p1 = origin.first(_p1.length)
        assert(p1 == _p1, "取前n个字符失败")
        var s1 = origin.last(_s1.length)
        assert(s1 == _s1, "取后n个字符失败")

        s1 = origin.from(origin.length - _s1.length)
        assert(s1 == _s1, "取后n个字符失败")
        
    }
}


func main(){
    YY.StringTest().test()
}

main()
